package behaviorPattern.observer.jdk9;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * @author : Alex Hu
 * date : 2020/3/22 上午11:25
 * description :
 */


public class BeanObserverTest extends JFrame implements PropertyChangeListener {
    BeanObserver testBean = new BeanObserver();
    boolean beanValue = false;

    public BeanObserverTest() {
        testBean.addPropertyChangeListener(this);
        JButton jb = new JButton("change");
        this.setTitle("");
        this.setBounds(100, 100, 100, 100);
        this.setLayout(new FlowLayout());
        this.add(jb);
        jb.addActionListener(e -> testBean.setProperty(!beanValue));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String args[]) {
        new BeanObserverTest();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getSource() == testBean) {
            beanValue = !beanValue;
            System.out.println("变化" + beanValue);
        }
    }
}